import axios from 'axios'
import _ from 'lodash'

axios.defaults.baseURL = 'http://139.199.206.204';
axios.defaults.timeout = 1000 * 15;
axios.defaults.headers['Content-Type'] = 'application/json';

axios.getJson = (url,parm = {}) => {
	url = newUrl(url,parm);
	return new Promise((resolve, reject) => {
	    axios.get(url).then((response) => {
	    	if( response.status == 200 ){
	    		resolve(response.data)
	    	}
	    }).catch((response) => {
	      	console.log('f', response)
	      	reject(response)
	    })
  	})
}

const newUrl = (url,parm) => {
	if(_.isEmpty(parm) || !_.isObject(parm)) {
		return url;
	}
	let c = ''
	_.forEach(parm, function(n, key) {
		if (_.isEmpty(c)) {
			c += key+'='+n
		} else {
			c += '&'+key+'='+n
		}
	});
	return url+'?'+c;
}

export default  axios